package leetcode.model;

/**
 * @author: feiwang_6
 * @create: 2020/7/19 10:43
 * @description: 二叉树节点定义
 */
public class TreeNode<T> {
    public T value;
    public TreeNode<T> left;
    public TreeNode<T> right;

    public TreeNode(T val) {
        this.value = val;
    }
    
    public void put(T val){
        put(val, this);
    }

    /**
     * 构建二叉树
     * @param val
     * @param root
     */
    private void put(T val, TreeNode<T> root){
        Comparable<T> rootVal = (Comparable<T>) root.value;
        if(rootVal.compareTo(val) >= 0){
            if(root.left == null){
                root.left = new TreeNode<T>(val);
                return;
            }
            put(val, root.left);
        }else {
            if(root.right == null){
                root.right = new TreeNode<T>(val);
                return;
            }
            put(val, root.right);
        }
    }
}
